﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using System.Data;
using System.Data.SqlClient;

namespace dao
{
    public class SanphamTrathatDao:BaseDao
    {
        public List<SanphamTrathatModel> getListSanphamByLoaiSanpham(long id)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_sptrathat_ByLoai";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add("@IDLoaisanpham", SqlDbType.BigInt).Value = id;
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<SanphamTrathatModel> glstSanpham = new List<SanphamTrathatModel>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        SanphamTrathatModel sp = new SanphamTrathatModel();
                        sp.ID = Convert.ToInt64(dr["ID"]);
                        sp.Masanpham = Convert.ToString(dr["Masanpham"]);
                        sp.Tensanpham = Convert.ToString(dr["Tensanpham"]);
                        sp.Mota = Convert.ToString(dr["Mota"]);
                        sp.Gia = Convert.ToDouble(dr["giaban"]);
                        sp.Anhdaidien = Convert.ToString(dr["anhdaidien"]);
                        sp.Soluong = Convert.ToInt64(dr["soluong"]);
                        sp.IDLoaisanpham = Convert.ToInt64(dr["IDLoaisanpham"]);
                        
                        glstSanpham.Add(sp);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstSanpham;
            }
        }

        public List<SanphamTrathatModel> getListSanphamByID(long id)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_sptrathat_SelectRow";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = id;
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<SanphamTrathatModel> glstSanpham = new List<SanphamTrathatModel>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        SanphamTrathatModel sp = new SanphamTrathatModel();
                        sp.ID = Convert.ToInt64(dr["ID"]);
                        sp.Masanpham = Convert.ToString(dr["Masanpham"]);
                        sp.Tensanpham = Convert.ToString(dr["Tensanpham"]);
                        sp.Mota = Convert.ToString(dr["Mota"]);
                        sp.Gia = Convert.ToDouble(dr["giaban"]);
                        sp.Anhdaidien = Convert.ToString(dr["anhdaidien"]);
                        sp.Soluong = Convert.ToInt64(dr["soluong"]);
                        sp.IDLoaisanpham = Convert.ToInt64(dr["IDLoaisanpham"]);

                        glstSanpham.Add(sp);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstSanpham;
            }
        }
    }
}
